﻿using AdminStyem.Application.Command.traceabilityCommand;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.traceabilityCommandHandler
{
    public class TraceabitlDelCommandHandler : IRequestHandler<TraceabitlDelCommand, APIResult<object>>
    {
        IBaseRespository<Traceability> BaseTrancebilty;

        IMapper mapper;

        public TraceabitlDelCommandHandler(IBaseRespository<Traceability> baseTrancebilty,IMapper mapper)
        {
            BaseTrancebilty = baseTrancebilty;
            this.mapper = mapper;
           
        }

        public Task<APIResult<object>> Handle(TraceabitlDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> ret =new APIResult<object>();
            ret.Code = ResultCode.删除成功;
            ret.Message =ResultCode.删除成功.ToString();
           
           var list= BaseTrancebilty.GetAll().Result.Where(x=>x.TraceabilityId == request.TraceabilityId).FirstOrDefault();

            if (list==null)
            {
                ret.Code = ResultCode.删除失败;
                ret.Message = ResultCode.删除失败.ToString();
                return Task.FromResult(ret);
            }
            else
            {
                list.IsDeleted = true;
                BaseTrancebilty.update(list);
                ret.Code = ResultCode.删除成功;
                ret.Message = ResultCode.删除成功.ToString();
                return Task.FromResult(ret);
            }

            return Task.FromResult(ret);
        }
    }
}
